package com.xy.biz.base.domain.converter;

import com.xy.biz.base.domain.bo.DictBO;
import com.xy.biz.base.domain.bo.DictPropertyBO;
import com.xy.biz.base.domain.dto.DictDTO;
import com.xy.biz.base.domain.po.DictPO;
import com.xy.biz.base.domain.query.DictQuery;
import com.xy.biz.base.domain.req.ListDictItemRequest;
import com.xy.biz.base.domain.req.ListDictRequest;
import com.xy.biz.base.domain.req.SaveDictRequest;
import com.xy.biz.base.domain.req.UpdateDictRequest;
import com.xy.tool.util.BeanUtilsEx;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 *
 *
 * @author zsp
 * @date 2020-5-24
 */
public class DictConverter {

    public static DictPO toPO(DictBO source) {
        return BeanUtilsEx.copyProperties(source, () -> new DictPO(), true);
    }

    public static List<DictPO> toPO(Collection<DictBO> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new DictPO(), true);
    }

    public static DictBO fromPO(DictPO source) {
        return BeanUtilsEx.copyProperties(source, () -> new DictBO());
    }

    public static List<DictBO> fromPO(Collection<DictPO> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new DictBO());
    }

    public static DictBO toBO(SaveDictRequest source) {
        DictBO dictBO = BeanUtilsEx.copyProperties(source, () -> new DictBO(), true);
        List<SaveDictRequest.DictProperty> propertyList = source.getPropertyList();
        if (!CollectionUtils.isEmpty(propertyList)) {
            dictBO.setPropertyList(BeanUtilsEx.copyPropertiesForNewList(propertyList, () -> new DictPropertyBO()));
        }
        return dictBO;
    }

    public static DictBO toBO(UpdateDictRequest source) {
        return BeanUtilsEx.copyProperties(source, () -> new DictBO(), true);
    }

    public static List<DictBO> toBO(Collection<SaveDictRequest> source) {
        return BeanUtilsEx.copyPropertiesForNewList(source, () -> new DictBO(), true);
    }

    public static DictDTO toDTO(DictBO source) {
        DictDTO dictDTO = BeanUtilsEx.copyProperties(source, () -> new DictDTO());
        if (source.getParentDict() != null) {
            dictDTO.setParentDict(BeanUtilsEx.copyProperties(source.getParentDict(), () -> new DictDTO()));
        }
        return dictDTO;
    }

    public static List<DictDTO> toDTO(Collection<DictBO> source) {
        List<DictDTO> target = new ArrayList<>();
        if (!CollectionUtils.isEmpty(source)) {
            Map<Long, DictDTO> parentDictDTOMap = new HashMap<>();
            for (DictBO dictBO : source) {
                DictDTO dictDTO = BeanUtilsEx.copyProperties(dictBO, () -> new DictDTO());
                if (dictBO.getParentDict() != null) {
                    dictDTO.setParentDict(parentDictDTOMap.computeIfAbsent(dictBO.getParentDictId(),
                            (e) -> BeanUtilsEx.copyProperties(dictBO.getParentDict(), () -> new DictDTO())));
                }
                target.add(dictDTO);
            }
        }
        return target;
    }

    public static DictQuery toQuery(ListDictRequest source) {
        if (null == source) {
            return null;
        }
        DictQuery target = new DictQuery(source.getPageIndex(), source.getPageSize());
        BeanUtilsEx.copyProperties(source, target);
        return target;
    }

    public static DictQuery toQuery(ListDictItemRequest source) {
        if (null == source) {
            return null;
        }
        DictQuery target = new DictQuery(source.getPageIndex(), source.getPageSize());
        BeanUtilsEx.copyProperties(source, target);
        return target;
    }

}
